#include<QDebug>
#include<QHostAddress>
#include<QTcpSocket>
#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
    m_pServer=NULL;
}

Widget::~Widget()
{
    delete ui;
}

void Widget::InitWidget()
{
    qDebug()<<"void Widget::InitWidget() start";
    if(NULL !=m_pServer)
    {
        return;
    }
    m_pServer=new QTcpServer(this);
    connect(m_pServer,SIGNAL(newConnection()),this,SLOT(OnNewConnection()));
    qDebug()<<"void Widget::InitWidget() end";
}

void Widget::OnNewConnection()
{
    qDebug()<<"Widget::OnNewConnection start";
    QTcpSocket *pSocket=m_pServer->nextPendingConnection();
    connect(pSocket,SIGNAL(readyRead()),this,SLOT(OnReadReady()));
//    ui->lineEdit_2->setText(m_pServer);
    qDebug()<<"Widget::OnNewConnection end";




}

void Widget::OnReadReady()
{
     qDebug()<<"Widget::OnReadReady start";
     QTcpSocket *pSocket=(QTcpSocket *) sender();
     QByteArray arr=pSocket->readAll();
     qDebug()<< pSocket<<" "<<arr;


     QString str="send data to client";


     ui->lineEdit_2->setText(arr);//显示
     qDebug()<<"Widget::OnReadReady end";
}

void Widget::on_pushButton_clicked()
{
    qDebug()<<"Widget::on_pushButton_clicked start";
    QString strIP=ui->edit_ip->text();
    QString strPort=ui->edit_port->text();
    m_pServer->listen(QHostAddress(strIP),strPort.toShort());
    qDebug()<<"Widget::on_pushButton_clicked end";
}


